package com.share.wechat.order.service.impl;

import com.share.common.tools.ID.IDutils;
import com.share.common.tools.collection.CollectionUtil;
import com.share.common.userinfo.UserUtil;
import com.share.wechat.core.entity.Detail;
import com.share.wechat.order.common.constant.OrderConstant;
import com.share.wechat.order.entity.OrderWithUser;
import com.share.wechat.order.entity.SalesOrder;
import com.share.wechat.order.repository.inter.OrderWithUserRepository;
import com.share.wechat.order.repository.inter.SalesOrderRepository;
import com.share.wechat.order.service.SalesOrderService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SalesOrderServiceImpl implements SalesOrderService {
    private final Logger logger = Logger.getLogger(this.getClass());
    @Resource
    private OrderWithUserRepository orderWithUserRepository;
    @Resource
    private SalesOrderRepository salesOrderRepository;

    @Override
    public List<String> getUserBuyDetailsId(String userInfo) {
        logger.debug("用户的id为" + userInfo);
        List<String> allByLists = orderWithUserRepository.findAllByUserInfo(userInfo);
        logger.debug("用户的浏览痕迹为" + allByLists);
        return allByLists;
    }


    @Override
    public List<Detail> getUserBuyDetails(String userInfo) {
        logger.debug("用户的id为" + userInfo);
        List<OrderWithUser> buyInfo = orderWithUserRepository.findByUserInfo(userInfo);
        //只取出所有的商品信息集合
        List<Object> allDetails = CollectionUtil.getListByFild(buyInfo, "detailId");




        return null;
    }


    @Override
    public SalesOrder getNewSaleOrder() {
        SalesOrder salesOrder = new SalesOrder();
        /*---------------------- 开始设置订单的部分属性 ----------------------*/
        if (StringUtils.isEmpty(salesOrder.getId())){
            logger.debug("该订单未有id信息");
            salesOrder.setId(IDutils.getUUID());
            logger.debug("产生的订单的id 信息为"+salesOrder.getId());
        }
        salesOrder.setCreateTime(new Date());//设置当前时间
        salesOrder.setIsValid(OrderConstant.ORDER_VALID_SUCCESS);//设置订单时间有效
        salesOrder.setUpdateTime(new Date());//更新时间
        salesOrder.setIsPay(OrderConstant.ORDER_PAY_NOT);//设置没有支付
        salesOrder.setUserInfo(UserUtil.getCurrentUserInfo());//设置当前用户信息
        salesOrder.setTotalNum(OrderConstant.NOT_ADD_ORDER);//设置0 表示没有添加商品
        salesOrderRepository.save(salesOrder);
        return salesOrder;
    }
}
